package com.motu.monstercity.server.game.logic;

import com.motu.monstercity.server.game.commondata.skill.Skill;
import com.motu.monstercity.server.game.commondata.skill.SkillEffectType;
import com.motu.monstercity.server.game.commondata.skill.SkillUpgradeType;
import com.motu.vertx.module.utility.mysql.MysqlClient;
import com.motu.vertx.module.utility.mysql.MysqlTool;
import com.motu.vertx.module.utility.toolset.Tool;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.json.JsonArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class SkillBaseData {
    private static Logger logger = LoggerFactory.getLogger(SkillBaseData.class);
    private static ConcurrentHashMap<Integer, SkillUpgradeType> skillUpgradeTypeMap = new ConcurrentHashMap<>(); // 技能升级的消耗数量配置表
    private static ConcurrentHashMap<Integer, Skill> skillMap = new ConcurrentHashMap<>(); // 技能配置表
    private static ConcurrentHashMap<Integer, SkillEffectType> skillEffectTypeMap = new ConcurrentHashMap<>(); // 技能加成类型配置说明表


    // 公共数据初始化
    public static void init(List<Future> futures) {
        futures.add(initSkill());
        futures.add(initSkillUpgradeType());
        futures.add(initSkillEffectType());
    }

    /**
     * 初始化技能升级的消耗数量配置表
     */
    public static Future<Void> initSkillUpgradeType() {
        String table = "skill_upgrade_type";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, SkillUpgradeType> tmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        SkillUpgradeType entity = new SkillUpgradeType(result.getJsonObject(i));
                        tmp.put(entity.getId(), entity);
                    }
                    skillUpgradeTypeMap = tmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static SkillUpgradeType getSkillUpgradeType(int id) {
        SkillUpgradeType result = skillUpgradeTypeMap.get(id);
        if (result == null) {
            logger.error("getSkillUpgradeType error key = " + id);
        }
        return result;
    }
    public static ConcurrentHashMap<Integer, SkillUpgradeType> getSkillUpgradeTypeMap() { return skillUpgradeTypeMap; }

    /**
     * 初始化技能配置表
     */
    public static Future<Void> initSkill() {
        String table = "skill";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, Skill> tmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        Skill entity = new Skill(result.getJsonObject(i));
                        tmp.put(entity.getSkillId(), entity);
                    }
                    skillMap = tmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static Skill getSkill(int id) {
        Skill result = skillMap.get(id);
        if (result == null) {
            logger.error("getSkill error key = " + id);
        }
        return result;
    }
    public static ConcurrentHashMap<Integer, Skill> getSkillMap() { return skillMap; }

    // 获取技能升级需要消耗的道具数量
    public static long getSkillLeveUpConsume(int upgradeType, int level) {
        for (SkillUpgradeType skillUpgradeType : getSkillUpgradeTypeMap().values()) {
            if (upgradeType == skillUpgradeType.getUpgradeType() && level == skillUpgradeType.getLevel()) {
                return skillUpgradeType.getConsume();
            }
        }
        return 0L;
    }

    /**
     * 初始化技能加成类型配置说明表
     */
    public static Future<Void> initSkillEffectType() {
        String table = "skill_effect_type";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, SkillEffectType> tmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        SkillEffectType entity = new SkillEffectType(result.getJsonObject(i));
                        tmp.put(entity.getEffectType(), entity);
                    }
                    skillEffectTypeMap = tmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static SkillEffectType getSkillEffectType(int effectType) {
        SkillEffectType result = skillEffectTypeMap.get(effectType);
        if (result == null) {
            logger.error("getSkillEffectType error key = " + effectType);
        }
        return result;
    }
    public static ConcurrentHashMap<Integer, SkillEffectType> getSkillEffectTypeMap() { return skillEffectTypeMap; }
}
